package com.ntd.study.base.thread.interrupt;

import cn.hutool.core.lang.Console;
import lombok.SneakyThrows;

/**
 * 线程中断demo
 */
public class DemoThreadInterrupt {
    static Runnable runnable = new Runnable() {
        @SneakyThrows
        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                synchronized (this) {
//                    wait(10000);
                }
                Thread.sleep(10000);
                Console.log("线程运行中");
            }
        }
    };

    public static void main(String[] args) throws InterruptedException {
        final Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(2000);
        thread.interrupt(); // 调用线程中断方法 - 线程在 wait 或者 sleep 状态都可以被中断
    }
}
